package com.wwj.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wwj.reggie.common.BaseContext;
import com.wwj.reggie.common.R;
import com.wwj.reggie.dto.OrdersDto;
import com.wwj.reggie.entity.OrderDetail;
import com.wwj.reggie.entity.Orders;
import com.wwj.reggie.service.OrderDetailService;
import com.wwj.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("");
    }

    /**
     * 历史订单及最新订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> latestOrderDetail(int page, int pageSize){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Orders::getUserId, userId); //过滤条件
        queryWrapper.orderByDesc(Orders::getCheckoutTime);//按买单时间排序

        ordersService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, ordersDtoPage,"records"); //对象拷贝

        //取出pageInfo中的records
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item, ordersDto);

            Long orderId = item.getId();

            LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
            lqw.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetails = orderDetailService.list(lqw);

            ordersDto.setOrderDetails(orderDetails);

            return ordersDto;

        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }

    /**
     * 订单明细分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime){
        log.info("page ==> {}, pageSize ==> {}, number ==> {}, beginTime ==> {}, endTime ==> {}", page, pageSize, number, beginTime, endTime);

        Page pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        lqw.between(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime, endTime);
        lqw.orderByDesc(Orders::getOrderTime);

        ordersService.page(pageInfo, lqw);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId, orders.getId());

        ordersService.update(orders, lqw);

        return R.success("");
    }
}
